using System;
using System.Xml.Serialization;
using System.ComponentModel;
using SharpMud.Server.Systems.Net.Telnet;

#if !Use_Common_MudLib
using SharpMud.MudLib.SAMPLE.Net;
using SharpMud.DynamicTyping;
#else
using SharpMud.MudLib.Common.Entities.Player.Session;
#endif

namespace SharpMud.MudLib.SAMPLE
{
	[XmlRoot("ServerConfiguration")]
	public class ServerConfiguration
	{
		public ServerConfiguration()
		{
		}

		public static ServerConfiguration Default
		{
			get
			{
				ServerConfiguration mlc = new ServerConfiguration();
				
				mlc.MudTimeConfig = Core.Time.MudTimeConfiguration.Default;
				
				mlc.TelnetServerConfig = new SharpMud.Server.Systems.Net.Telnet.TelnetServerConfiguration<TelnetPlayerSession>();
				mlc.TelnetServerConfig.BasePort = 4000;

                AssembliedTypeLocation atlDefaultTelnetSessionState = SharpMud.DynamicTyping.AssembliedTypeLocation.FromLoadedType(typeof(Net.SessionStates.NewConnection));

				mlc.TelnetServerConfig.ListenersCollection.Add(
					new ListenerMetadatum<TelnetPlayerSession>(
						new SharpMud.Server.Systems.Net.SLU("telnet",System.Net.IPAddress.Any,"+0")
					,   atlDefaultTelnetSessionState )        
                                                              );

				mlc.CommandManagerConfig = Commands.CommandManagerConfiguration.Default;

				return mlc;
			}
		}
//		public static Server.Systems.Network.NetworkSystemConfiguration DefaultNetworkConfig
//		{
//			get
//			{
//				Server.Systems.Network.NetworkSystemConfiguration dnc = new SharpMud.Server.Systems.Network.NetworkSystemConfiguration();
//				
//				dnc.BasePort = 4000;
//				
//				//Create a default telnet listener
//				Server.Systems.Network.ListenerMetadata lm = new SharpMud.Server.Systems.Network.ListenerMetadata();
//				lm.PortString = "+0";
//				lm.DefaultConnectionHandler = SharpMud.DynamicTyping.AssembliedTypeLocation.FromLoadedType(typeof(SAMPLE.Network.Handlers.NewTelnetConnectionHandler));
//				
//				//Assign in the default telnet listener
//				dnc.Listeners = new Server.Systems.Network.ListenerMetadata[] 
//					{lm};
//
//				//Create a default rule that will accept any connection, or at least do nothing
//				//to it when it passes through
//				Server.Systems.Network.ConnectionHandlingRule defConnectionRule = new SharpMud.Server.Systems.Network.ConnectionHandlingRule();
//				defConnectionRule.LocalEndpoint = new SharpMud.Server.Systems.Network.EndpointRule(
//					new Text.Matching.StringMatcher(Text.Matcheing.StringMatchingType.StringLike,"*")
//					,
//					new Text.Matching.StringMatcher(Text.Matcheing.StringMatchingType.StringLike,"*")
//					);
//				defConnectionRule.RemoteEndpoint = new SharpMud.Server.Systems.Network.EndpointRule(
//					new Text.Matching.StringMatcher(Text.Matcheing.StringMatchingType.StringLike,"*")
//					,
//					new Text.Matching.StringMatcher(Text.Matcheing.StringMatchingType.StringLike,"*")
//					);
//				defConnectionRule.Handler = DynamicTyping.AssembliedTypeLocation.FromLoadedType(typeof(Server.Systems.Network.Handlers.DoNothing));
//
//
//				//Assign the default connection rule into the security configuration
//				dnc.Security = new SharpMud.Server.Systems.Network.NetworkSystemSecurityConfiguration();
//				dnc.Security.NewConnectionRules = new Server.Systems.Network.ConnectionHandlingRule[] 
//					{defConnectionRule};
//
//				//Okay! The configuration is done being configured, so return it
//				return dnc;
//			}
//		}

		private Core.Time.MudTimeConfiguration								_MudTimeConfig;
        private SharpMud.Server.Systems.Net.Telnet.TelnetServerConfiguration<TelnetPlayerSession> _TelnetServerConfig;
        private Commands.CommandManagerConfiguration						_CommandManagerConfig;

		[XmlElement("MudTimeConfig")]
		[DefaultValue( typeof(Core.Time.MudTimeConfiguration), "" )]
		public Core.Time.MudTimeConfiguration MudTimeConfig
		{
			get
			{
				return _MudTimeConfig;
			}
			set
			{
				_MudTimeConfig = value;
			}
		}

		[XmlElement("TelnetServerConfig")]
		public SharpMud.Server.Systems.Net.Telnet.TelnetServerConfiguration<TelnetPlayerSession> TelnetServerConfig
		{
			get
			{
				return _TelnetServerConfig;
			}
			set
			{
				_TelnetServerConfig = value;
			}
		}

		[XmlElement("CommandManagerConfig")]
		[DefaultValue( typeof(Commands.CommandManagerConfiguration), "" )]
		public Commands.CommandManagerConfiguration CommandManagerConfig
		{
			get
			{
				return _CommandManagerConfig;
			}
			set
			{
				_CommandManagerConfig = value;
			}
		}
	}
}
